package action.login;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import security.MySecurityContextHandle;
import service.interf.security.IGroupMember;
import service.interf.security.IMenu;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.opensymphony.xwork2.Action;

import dao.interf.IMemberDAO;
import dao.pojo.Group;
import dao.pojo.Member;
import dao.pojo.Menu;

public class AdminAction implements Action {
	private String url;
	private IMenu menuImp;
	private IGroupMember groupMemberImpl;
	private IMemberDAO memberDao;
	public static Map<String, String> snMap;

	public IGroupMember getGroupMemberImpl() {
		return groupMemberImpl;
	}

	public void setGroupMemberImpl(IGroupMember groupMemberImpl) {
		this.groupMemberImpl = groupMemberImpl;
	}

	public IMenu getMenuImp() {
		return menuImp;
	}

	public void setMenuImp(IMenu menuImp) {
		this.menuImp = menuImp;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setMemberDao(IMemberDAO memberDao) {
		this.memberDao = memberDao;
	}

	public String execute() throws Exception {
		// 清除session
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("userId");
		session.removeAttribute("userName");
		// session.removeAttribute("deviceBindCode");
		session.removeAttribute("loginUrl");
		session.removeAttribute("menuList");
		Long userId = MySecurityContextHandle.getUserId();
		if (null == userId) {
			url = "/cloud_ifw/j_spring_security_logout";
			return "success";
		}
		// 用户信息
		Member member = memberDao.findById(userId);
		if (null != member) {
			session.setAttribute("userId", userId);
			session.setAttribute("userName", member.getUserName());
			// session.setAttribute("deviceBindCode",
			// member.getDeviceBindCode());
		}
		// 当前登录用户的组信息和菜单信息
		List<Group> gL = groupMemberImpl.getGroupByUserId(userId);
		for (Group g : gL) {
			String loginUrl = g.getLoginUrl();
			if (!StringUtils.isEmpty(loginUrl)) {
				url = loginUrl;
				break;
			}
		}
		if (StringUtils.isEmpty(url)) {
			url = "/pages/web/index.jsp";
		}
		session.setAttribute("loginUrl", url);
		Map<String, List<Menu>> menuMap = menuImp.getGroupMenusByUserId(gL);
		session.setAttribute("menuList", menuMap);
		// 获取sn数据
		String url = "jdbc:mysql://www.ifw8.cn:3306/fw";
		String username = "admin";
//		String password = "2016ifwadmin";
		String password = "web2267719206";
		Connection conn = null;
		conn = (Connection) DriverManager
				.getConnection(url, username, password);
		String sql = "select * from ifw_srouter_map";
		PreparedStatement pstmt;
		pstmt = (PreparedStatement) conn.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		snMap = new HashMap<String, String>();
		while (rs.next()) {
			String fwsn = rs.getString("fwsn");
			String sn = rs.getString("sn");
			snMap.put(sn, fwsn);
		}
		return "success";
	}
}
